﻿using CQRSFramework.Commands;
using CQRSFramework.Common.Components;
using CQRSFramework.Common.Configurations;
using CQRSFramework.Common.JsonNet;
using CQRSFramework.Common.Utilities;
using CQRSFramework.Extensions;
using CQRSFramework.MySql;
using CQRSFramework.RabbitMQ;
using CQRSFramework.Test.commands;
using System;
using System.Diagnostics;
using System.Threading.Tasks;

namespace CQRSFramework.Test
{
    class Program
    {
        static void Main(string[] args)
        {
            IntiCommon();
            var commandBus = ObjectContainer.Resolve<ICommandBus>();

            //var createAccount = new CreateAccountCommand { AggregateRootId = Guid.NewGuid().ToString(), UserId = "101", Name = "1234" };
            //var createAccount1 = new CreateAccountCommand { AggregateRootId = Guid.NewGuid().ToString(), UserId = "101", Name = "1234" };
            //Stopwatch sw = new Stopwatch();
            //sw.Start();
            //commandBus.Publish(createAccount);
            //commandBus.Publish(createAccount1);
            //sw.Stop();
            //Console.WriteLine("Elapsed :" + sw.ElapsedMilliseconds);
            //sw.Reset();
            //for (int i = 0; i < 10; i++)
            //{
            //    Console.WriteLine(ObjectId.GenerateNewId());
            //}
            Parallel.For(0, 1000, _ =>
            {
                //int _ = 1;
                var id = Guid.NewGuid().ToString();
                var aggid = id;
                var acc = new CreateAccountCommand { AggregateRootId = aggid, UserId = "101" + _, Name = "1234" };
                commandBus.Publish(acc);
                var createAccount1 = new ChangeAccountNameCommand { AggregateRootId = aggid, UserId = "101" + _, Name = "caochangjiang" };
                commandBus.Publish(createAccount1);
                var createAccount12 = new ChangeAccountNameCommand { AggregateRootId = aggid, UserId = "101" + _, Name = "caochangjiang2" };
                commandBus.Publish(createAccount12);
                var createAccount13 = new ChangeAccountNameCommand { AggregateRootId = aggid, UserId = "101" + _, Name = "caochangjiang3" };
                commandBus.Publish(createAccount13);
                var createAccount14 = new ChangeAccountNameCommand { AggregateRootId = aggid, UserId = "101" + _, Name = "caochangjiang4" };
                commandBus.Publish(createAccount14);
                var createAccount15 = new ChangeAccountNameCommand { AggregateRootId = aggid, UserId = "101" + _, Name = "caochangjiang4" };
                commandBus.Publish(createAccount15);
                var createAccount16 = new ChangeAccountNameCommand { AggregateRootId = aggid, UserId = "101" + _, Name = "caochangjiang6" };
                commandBus.Publish(createAccount16);
                var createAccount17 = new ChangeAccountNameCommand { AggregateRootId = aggid, UserId = "101" + _, Name = "caochangjiang7" };
                commandBus.Publish(createAccount17);
            });
            //Console.WriteLine("Elapsed :" + sw.ElapsedMilliseconds);
            //var changeName = new ChangeAccountNameCommand { AggregateRootId = aggregateRootId, Name = "a1" };
            //commandBus.PublishAsync(changeName).Wait();
            Console.ReadKey();
        }
        static void IntiCommon()
        {
            var assemblies = System.AppDomain.CurrentDomain.GetAssemblies();
            CommonConfiguration.Create()
                .UseAutofac()
                .UseJsonNet()
                .RegisterCQRSFrameworkAssemblies(assemblies)
                .RegisterCommonComponents()
                .UseMySQLEventStore()
                .UseRabbitMQ("localhost", "guest", "guest", 5672, "/", "CityGrid")
                .Build()
                .InitMySQLEventStore("Server =10.0.1.195 ; Port = 3306; Database = eventstore; Uid = root; Pwd = 123456; charset = utf8; SslMode = None")
                .StartRabbitMQSubscriber(Constants.CommandTopic, Constants.EventTopic);
        }
    }
}
